%%% Copyright [heng] [litaocheng@gmail.com] 
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License. 
%%% You may obtain a copy of the License at 
%%%    http://www.apache.org/licenses/LICENSE-2.0 
%%% Unless required by applicable law or agreed to in writing, 
%%% software distributed under the License is distributed on an "AS IS" BASIS, 
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
%%% See the License for the specific language governing permissions and 
%%% limitations under the License. 
-include_lib("kernel/include/inet.hrl").


%%% type defines

% error type
-type ereason() :: atom().

% erlang:now/0
-type timetick() :: {non_neg_integer(), non_neg_integer(), non_neg_integer()}. 

% work
-type worker_state() :: 'init' | 'connecting' | 'connected' | 'closed'.
-type worker_name() :: atom().
-type host() :: atom().

% job
-type job_id() :: non_neg_integer().
-type job_bin() :: binary().
-type job_state() :: 'init' | 'map' | 'reduce' | 'cancel' | 'complete'.

% map
-type map_state() :: 'idle' | 'read' | 'progress' | 'complete'.

% reduce
-type reduce_state() :: 'idle' | 'read' | 'progress' | 'complete'.

%% the work info
-record(worker, 
        {          
          node = '' :: node(),      % the worker node name(equal with erlang node)
          name = '' :: worker_name(),% the wroker name
          host = '' :: host(),      % the worker host, name@host equal with node
          ip = "" :: string(),      % the ip string
          ssh_name = '' :: atom(),  % the ssh name
          state = 'init' :: worker_state(), % the worker state           
          repsetting = [],          % report setting
          replast = []              % the lastest report info
        }).

%% the  task info on the worker
-record(worker_task,
        {
          node = '' :: node(),      % the worker node name(equal with erlang node)
          maps,                     % the maps info
          reduces                   % the reduces info
        }).

%% the job
-record(job,
        {
          id = 0 :: job_id(),               % job id
          mod = '' :: atom(),               % job module
          bin = <<>> :: job_bin(),          % job binary code
          conf = [] :: list(),              % job config
          state = '' :: job_state(),        % job state
          startt = erlang:now() :: timetick() % start time
        }).

%% the map task info
-record(map_task,
        {
          jobid = 0 ::job_id(),             % job id
          node = '' :: node(),              % on which worker?
          index = 0 :: non_neg_integer(),   % map index
          state = 'idle' :: map_state(),    % map state
          startt = erlang:now() :: timetick() % start time
        }).

%% the reduce task info
-record(reduce_task,
        {
          jobid = 0 :: job_id(),            % job id
          node = '' :: node(),              % on which worker?
          index = 0 :: non_neg_integer(),   % reduce index
          state = 'idle' :: reduce_state(), % reduce state
          startt = erlang:now() :: timetick() % start time
        }).

-type worker() :: #worker{}.
-type job() :: #job{}.
-type map_task() :: #map_task{}.
-type reduce_task() :: #reduce_task{}.
